package com.cgbl.cg.ui.mine

import android.os.Bundle
import android.transition.Explode
import android.view.Window
import android.view.WindowManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.FragmentUtils
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.databinding.AppActivityResourceProfileBinding
import com.cgbl.cg.vm.mine.CourseProfileViewModel
import com.cgbl.cg.vm.mine.ResourceProfileViewModel
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.CourseInfoFileBean
import com.robot.base.util.LiveDataEventManager


@Route(path = AppConstants.Router.Mine.A_MINE_RESOURCE_PROFILE)
class ResourceProfileActivity :
    GLBaseActivity<AppActivityResourceProfileBinding, ResourceProfileViewModel>() {

    val courseProfileFragment = ResourceProfileFragment()

    val courseDirectoryFragment = ResourceParamsFragment()

    val courseCommentFragment = ResourceCommentFragment()

    val courseFileFragment = CourseResourceFragment()
    var id = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        // 设置一个exit transition
        window.requestFeature(Window.FEATURE_CONTENT_TRANSITIONS)
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.enterTransition = Explode()
        window.exitTransition = Explode()
        super.onCreate(savedInstanceState)
    }

    override fun initComponents() {
        initView()
        viewModel.fetchResourceInfo(id)
    }

    override fun initParam() {
        super.initParam()
        id = intent.getStringExtra("id") ?: ""
    }

    override fun initData() {
        super.initData()
        viewModel.id = id
    }

    private fun initView() {
        FragmentUtils.add(
            supportFragmentManager, listOf(
                courseProfileFragment,
                courseDirectoryFragment,
                courseCommentFragment,
                courseFileFragment
            ), R.id.fragment_container, 0
        )
        binding.tvMenu2.text = "参数"
        binding.tvTitle.text = "资源详情"
    }

    override fun initViewObservable() {
        LiveDataEventManager.observerPaySuccessEvent(this) {
            viewModel.fetchResourceInfo(id)
        }
        viewModel.uc.resourceInfoData.observe(this) {
            it ?: return@observe
            courseProfileFragment.setCourseProfileData(it)
            courseDirectoryFragment.setCourseProfileData(it)
            courseCommentFragment.setCourseProfileData(id)
            courseFileFragment.setCourseProfileData(it.naturalFiles as MutableList<CourseInfoFileBean>?, it.pay, it.id, "2")
        }
        viewModel.uc.switchTabEvent.observe(this) {
            when (it) {
                CourseProfileViewModel.TAB_PROFILE -> {
                    FragmentUtils.showHide(
                        courseProfileFragment,
                        courseDirectoryFragment,
                        courseCommentFragment,
                        courseFileFragment
                    )
                }
                CourseProfileViewModel.TAB_DIRECTORY -> {
                    FragmentUtils.showHide(
                        courseDirectoryFragment,
                        courseProfileFragment,
                        courseCommentFragment,
                        courseFileFragment
                    )
                }
                CourseProfileViewModel.TAB_COMMENT -> {
                    FragmentUtils.showHide(
                        courseCommentFragment,
                        courseProfileFragment,
                        courseDirectoryFragment,
                        courseFileFragment
                    )
                }
                CourseProfileViewModel.TAB_FILE -> {
                    FragmentUtils.showHide(
                        courseFileFragment,
                        courseProfileFragment,
                        courseDirectoryFragment,
                        courseCommentFragment
                    )
                }
            }
        }
    }

    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_resource_profile

    override fun initVariableId(): Int = BR.viewModel
}